﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace WCSY.DAL
{
    public class fg_approveFlowMstAppService
    {
        public fg_approveFlowMstAppService()
		{}
		#region  Method


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string sysno)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from fg_approveFlowMstApp");
			strSql.Append(" where sysno='"+sysno+"' ");
			return DBHelper.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public string Add(WCSY.MODEL.fg_approveFlowMstApp model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.sysno != null)
			{
				strSql1.Append("sysno,");
				strSql2.Append("'"+model.sysno+"',");
			}
			if (model.approvName != null)
			{
				strSql1.Append("approvName,");
				strSql2.Append("'"+model.approvName+"',");
			}
			if (model.opNo != null)
			{
				strSql1.Append("opNo,");
				strSql2.Append("'"+model.opNo+"',");
			}
			if (model.fillPsn != null)
			{
				strSql1.Append("fillPsn,");
				strSql2.Append("'"+model.fillPsn+"',");
			}
			if (model.filldate != null)
			{
				strSql1.Append("filldate,");
				strSql2.Append("'"+model.filldate+"',");
			}
			if (model.isEmail != null)
			{
				strSql1.Append("isEmail,");
				strSql2.Append(""+model.isEmail+",");
			}
			if (model.isMobile != null)
			{
				strSql1.Append("isMobile,");
				strSql2.Append(""+model.isMobile+",");
			}
			if (model.remarks != null)
			{
				strSql1.Append("remarks,");
				strSql2.Append("'"+model.remarks+"',");
			}
			strSql.Append("insert into fg_approveFlowMstApp(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			return strSql.ToString();
			
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public string Update(WCSY.MODEL.fg_approveFlowMstApp model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update fg_approveFlowMstApp set ");
			if (model.approvName != null)
			{
				strSql.Append("approvName='"+model.approvName+"',");
			}
			else
			{
				strSql.Append("approvName= null ,");
			}
			if (model.opNo != null)
			{
				strSql.Append("opNo='"+model.opNo+"',");
			}
			else
			{
				strSql.Append("opNo= null ,");
			}
			if (model.fillPsn != null)
			{
				strSql.Append("fillPsn='"+model.fillPsn+"',");
			}
			else
			{
				strSql.Append("fillPsn= null ,");
			}
			if (model.filldate != null)
			{
				strSql.Append("filldate='"+model.filldate+"',");
			}
			else
			{
				strSql.Append("filldate= null ,");
			}
			if (model.isEmail != null)
			{
				strSql.Append("isEmail="+model.isEmail+",");
			}
			else
			{
				strSql.Append("isEmail= null ,");
			}
			if (model.isMobile != null)
			{
				strSql.Append("isMobile="+model.isMobile+",");
			}
			else
			{
				strSql.Append("isMobile= null ,");
			}
			if (model.remarks != null)
			{
				strSql.Append("remarks='"+model.remarks+"',");
			}
			else
			{
				strSql.Append("remarks= null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where sysno='"+ model.sysno+"' ");
			return strSql.ToString();
			
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string sysno)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from fg_approveFlowMstApp ");
			strSql.Append(" where sysno='"+sysno+"' " );
			int rowsAffected=DBHelper.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string sysnolist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from fg_approveFlowMstApp ");
			strSql.Append(" where sysno in ("+sysnolist + ")  ");
			int rows=DBHelper.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public WCSY.MODEL.fg_approveFlowMstApp GetModel(string sysno)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" sysno,approvName,opNo,fillPsn,filldate,isEmail,isMobile,remarks ");
			strSql.Append(" from fg_approveFlowMstApp ");
			strSql.Append(" where sysno='"+sysno+"' " );
			WCSY.MODEL.fg_approveFlowMstApp model=new WCSY.MODEL.fg_approveFlowMstApp();
			DataSet ds=DBHelper.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["sysno"]!=null && ds.Tables[0].Rows[0]["sysno"].ToString()!="")
				{
					model.sysno=ds.Tables[0].Rows[0]["sysno"].ToString();
				}
				if(ds.Tables[0].Rows[0]["approvName"]!=null && ds.Tables[0].Rows[0]["approvName"].ToString()!="")
				{
					model.approvName=ds.Tables[0].Rows[0]["approvName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["opNo"]!=null && ds.Tables[0].Rows[0]["opNo"].ToString()!="")
				{
					model.opNo=ds.Tables[0].Rows[0]["opNo"].ToString();
				}
				if(ds.Tables[0].Rows[0]["fillPsn"]!=null && ds.Tables[0].Rows[0]["fillPsn"].ToString()!="")
				{
					model.fillPsn=ds.Tables[0].Rows[0]["fillPsn"].ToString();
				}
				if(ds.Tables[0].Rows[0]["filldate"]!=null && ds.Tables[0].Rows[0]["filldate"].ToString()!="")
				{
					model.filldate=DateTime.Parse(ds.Tables[0].Rows[0]["filldate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["isEmail"]!=null && ds.Tables[0].Rows[0]["isEmail"].ToString()!="")
				{
					model.isEmail=int.Parse(ds.Tables[0].Rows[0]["isEmail"].ToString());
				}
				if(ds.Tables[0].Rows[0]["isMobile"]!=null && ds.Tables[0].Rows[0]["isMobile"].ToString()!="")
				{
					model.isMobile=int.Parse(ds.Tables[0].Rows[0]["isMobile"].ToString());
				}
				if(ds.Tables[0].Rows[0]["remarks"]!=null && ds.Tables[0].Rows[0]["remarks"].ToString()!="")
				{
					model.remarks=ds.Tables[0].Rows[0]["remarks"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

        /// <summary>
        /// 得到一个对象实体 根据单据对象代码
        /// </summary>
        public WCSY.MODEL.fg_approveFlowMstApp GetModelByOpNo(string opNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" sysno,approvName,opNo,fillPsn,filldate,isEmail,isMobile,remarks ");
            strSql.Append(" from fg_approveFlowMstApp ");
            strSql.Append(" where opNo='" + opNo + "' ");
            WCSY.MODEL.fg_approveFlowMstApp model = new WCSY.MODEL.fg_approveFlowMstApp();
            DataSet ds = DBHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["sysno"] != null && ds.Tables[0].Rows[0]["sysno"].ToString() != "")
                {
                    model.sysno = ds.Tables[0].Rows[0]["sysno"].ToString();
                }
                if (ds.Tables[0].Rows[0]["approvName"] != null && ds.Tables[0].Rows[0]["approvName"].ToString() != "")
                {
                    model.approvName = ds.Tables[0].Rows[0]["approvName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["opNo"] != null && ds.Tables[0].Rows[0]["opNo"].ToString() != "")
                {
                    model.opNo = ds.Tables[0].Rows[0]["opNo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["fillPsn"] != null && ds.Tables[0].Rows[0]["fillPsn"].ToString() != "")
                {
                    model.fillPsn = ds.Tables[0].Rows[0]["fillPsn"].ToString();
                }
                if (ds.Tables[0].Rows[0]["filldate"] != null && ds.Tables[0].Rows[0]["filldate"].ToString() != "")
                {
                    model.filldate = DateTime.Parse(ds.Tables[0].Rows[0]["filldate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["isEmail"] != null && ds.Tables[0].Rows[0]["isEmail"].ToString() != "")
                {
                    model.isEmail = int.Parse(ds.Tables[0].Rows[0]["isEmail"].ToString());
                }
                if (ds.Tables[0].Rows[0]["isMobile"] != null && ds.Tables[0].Rows[0]["isMobile"].ToString() != "")
                {
                    model.isMobile = int.Parse(ds.Tables[0].Rows[0]["isMobile"].ToString());
                }
                if (ds.Tables[0].Rows[0]["remarks"] != null && ds.Tables[0].Rows[0]["remarks"].ToString() != "")
                {
                    model.remarks = ds.Tables[0].Rows[0]["remarks"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select sysno,approvName,opNo,fillPsn,filldate,isEmail,isMobile,remarks ");
			strSql.Append(" FROM fg_approveFlowMstApp ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DBHelper.Query(strSql.ToString());
		}

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public WCSY.MODEL.fg_approveFlowMstApp GetModelByOpno(string opNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" sysno,approvName,opNo,fillPsn,filldate,isEmail,isMobile,remarks ");
            strSql.Append(" from fg_approveFlowMstApp ");
            strSql.Append(" where opNo='" + opNo + "' ");
            WCSY.MODEL.fg_approveFlowMstApp model = new WCSY.MODEL.fg_approveFlowMstApp();
            DataSet ds = DBHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["sysno"] != null && ds.Tables[0].Rows[0]["sysno"].ToString() != "")
                {
                    model.sysno = ds.Tables[0].Rows[0]["sysno"].ToString();
                }
                if (ds.Tables[0].Rows[0]["approvName"] != null && ds.Tables[0].Rows[0]["approvName"].ToString() != "")
                {
                    model.approvName = ds.Tables[0].Rows[0]["approvName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["opNo"] != null && ds.Tables[0].Rows[0]["opNo"].ToString() != "")
                {
                    model.opNo = ds.Tables[0].Rows[0]["opNo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["fillPsn"] != null && ds.Tables[0].Rows[0]["fillPsn"].ToString() != "")
                {
                    model.fillPsn = ds.Tables[0].Rows[0]["fillPsn"].ToString();
                }
                if (ds.Tables[0].Rows[0]["filldate"] != null && ds.Tables[0].Rows[0]["filldate"].ToString() != "")
                {
                    model.filldate = DateTime.Parse(ds.Tables[0].Rows[0]["filldate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["isEmail"] != null && ds.Tables[0].Rows[0]["isEmail"].ToString() != "")
                {
                    model.isEmail = int.Parse(ds.Tables[0].Rows[0]["isEmail"].ToString());
                }
                if (ds.Tables[0].Rows[0]["isMobile"] != null && ds.Tables[0].Rows[0]["isMobile"].ToString() != "")
                {
                    model.isMobile = int.Parse(ds.Tables[0].Rows[0]["isMobile"].ToString());
                }
                if (ds.Tables[0].Rows[0]["remarks"] != null && ds.Tables[0].Rows[0]["remarks"].ToString() != "")
                {
                    model.remarks = ds.Tables[0].Rows[0]["remarks"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" sysno,approvName,opNo,fillPsn,filldate,isEmail,isMobile,remarks ");
			strSql.Append(" FROM fg_approveFlowMstApp ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DBHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM fg_approveFlowMstApp ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DBHelper.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.sysno desc");
			}
			strSql.Append(")AS Row, T.*  from fg_approveFlowMstApp T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DBHelper.Query(strSql.ToString());
		}
        /// <summary>
        /// 获取审批流设置主表中所有数据，联表查询
        /// </summary>
        /// <returns></returns>
        public DataTable getAllListBySql() {

            string sql = @"select sysno,approvname,mstapp.opno,operapp.opname,fillpsn,filldate,isemail,ismobile,mstapp.remarks 
            from fg_approveFlowMstApp as mstapp
            left join fg_operatesApp as operapp on mstapp.opno = operapp.opno";

            return DBHelper.GetDataTable(sql);
        }
		/*
		*/

		#endregion  Method
    }
}
